﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoadIn.Maps.Bing.Models;
using LoadIn.Maps.Bing.Services;
using LoadIn.Maps.Bing.ViewModels;
using Orchard.Caching;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Environment.Extensions;
using Orchard.Localization;

namespace LoadIn.Maps.Bing.Drivers
{
    [OrchardFeature("LoadIn.Maps.Bing")]
    public class BingMapsPartDriver : ContentPartDriver<BingMapsPart>
    {
        private const string TemplateName = "Parts/LoadIn.BingMaps";

        public Localizer T { get; set; }

        private readonly IBingMapsService _bingmapsService;

        public BingMapsPartDriver(IBingMapsService bingmapsService)
        {
            _bingmapsService = bingmapsService;
            T = NullLocalizer.Instance;
        }

        protected override DriverResult Display(BingMapsPart part, string displayType, dynamic shapeHelper)
        {
            string mapmode = _bingmapsService.GetMode();
            if (string.IsNullOrWhiteSpace(mapmode))
                mapmode = "Static";
            part.MapMode = mapmode;

            string apikey = _bingmapsService.GetApiKey();
            part.ApiKey = apikey;

            return ContentShape("Parts_Loadin_BingMaps",
                                () => shapeHelper.Parts_Loadin_BingMaps(
                                            Longitude: part.Longitude,
                                            Latitude: part.Latitude,
                                            ApiKey: part.ApiKey,
                                            MapMode: part.MapMode,
                                            Mode: part.Mode,
                                            PinIcon: part.PinIcon,
                                            PinText: part.PinText,
                                            Width: part.Width,
                                            Height: part.Height,
                                            Zoom: part.Zoom
                                        ));
        }

        //GET
        protected override DriverResult Editor(BingMapsPart part, dynamic shapeHelper)
        {
            string mapmode = _bingmapsService.GetMode();
            if (string.IsNullOrWhiteSpace(mapmode))
                mapmode = "Static";
            part.MapMode = mapmode;

            string apikey = _bingmapsService.GetApiKey();
            part.ApiKey = apikey;

            return ContentShape("Parts_Loadin_BingMaps_Edit",
                                () => shapeHelper.EditorTemplate(
                                                    TemplateName: TemplateName,
                                                    Model: part,
                                                    Prefix: Prefix));
        }

        //POST
        protected override DriverResult Editor(BingMapsPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }
}